package com.minisoft.utils;

import cn.hutool.core.io.FileUtil;
import cn.hutool.db.Db;
import com.minisoft.entity.CustomerBehavior;
import lombok.extern.slf4j.Slf4j;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * @author JueSu
 * @description: 检查到访客户差异
 * @date 2021/11/3 9:44
 */
@Slf4j
public class ImportBehav {

    public static String DB_CENTER = "center-pro";

    public static void main(String[] args) throws SQLException {
        List<String> lines = FileUtil.readLines("d:/output.txt", "utf-8");
        Db.use(DB_CENTER).executeBatch(lines);
    }

    /**
     * 每1000个生成一个insert 语句
     */
    public void testGenSql() {
        StringBuilder sqlBuffer = new  StringBuilder();
        Collection<CustomerBehavior> values = null;
        int count = 0;
        List<String> lines = new ArrayList<>();
        for (CustomerBehavior behavior : values) {
            if (behavior.getDescInfo().length() > 128) {
                continue;
            }
            // 每条记录的参数拼接
            sqlBuffer.append("('")
                    .append(behavior.getEventName()).append("', '")
                    .append(behavior.getMobile()).append("', '")
                    .append(behavior.getCityGps()).append("', '")
                    .append(behavior.getDescInfo()).append("', '")
                    .append(behavior.getRemark()).append("', '")
                    .append(behavior.getCreateTime()).append("', '")
                    .append(behavior.getUpdateTime()).append("'),");
            count++;

            // 当计数器到达1000时拼接一次INSERT语句
            if (count == 1000) {
                String sql = "INSERT INTO customer_behavior (event_name, mobile, city_gps, desc_info, remark, create_time, update_time) VALUES " + sqlBuffer.toString().substring(0, sqlBuffer.length() - 1) + ";";
                lines.add(sql);

                // 重置计数器和拼接字符串
                count = 0;
                sqlBuffer.setLength(0);
            }
        }

        if (count > 0) {
            String sql = "INSERT INTO customer_behavior (event_name, mobile, city_gps, desc_info, remark, create_time, update_time) VALUES " + sqlBuffer.toString().substring(0, sqlBuffer.length() - 1) + ";";
            lines.add(sql);
        }

        FileUtil.writeLines(lines,"d:/output.txt",  "utf-8");
    }

}
